﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using LJF.MSSQL;
using Microsoft.AspNetCore.Mvc;
using LJF.Models;
using NLog;
using Microsoft.AspNetCore.Authorization;
using LJF.Models.Models;

namespace LJF.ManageSYS.Controllers
{
    [Route("api/[controller]/[action]")]
    public class IndexController : BaseController
    {
        static Logger Logger = LogManager.GetCurrentClassLogger();

        ManageSys_DATAContext dataMD;

        public IndexController(ManageSys_DATAContext _dataMd)
        {
            dataMD = _dataMd;
        }

        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> GetMenus()
        {
            SystemMenu rootNode = new SystemMenu()
            {
                Id = -1,
                Icon = "",
                Href = "",
                Title = "根目录"
            };

            List<XtMenu> xtMenuEntities = new List<XtMenu>();
            xtMenuEntities = dataMD.XtMenu.Where(p => p.IsLogo == false && p.IsHome == false).ToList();

            //查询主菜单
            GetTreeNodeListByNoLockedDTOArray(xtMenuEntities.ToArray(), rootNode);

            //定义整个菜单节点
            MenusInfoResultDTO menusInfoResultDTO = new MenusInfoResultDTO();
            menusInfoResultDTO.MenuInfo = rootNode.Child;

            //查询Logo和Home节点数据
            XtMenu logo = dataMD.XtMenu.FirstOrDefault(p => p.IsLogo == true);
            XtMenu home = dataMD.XtMenu.FirstOrDefault(p => p.IsHome == true);
            menusInfoResultDTO.LogoInfo = new LogoInfo()
            {
                title = logo.Title,
                href = logo.Href,
                image = logo.Image
            };

            menusInfoResultDTO.HomeInfo = new HomeInfo()
            {
                href = home.Href,
                title = home.Title
            };

            return new JsonResult(menusInfoResultDTO);
        }

        public ResponseData GetAccountInfo()
        {
            object userInfo = dataMD.XtUser.Where(p => p.Id == 1).Select(u => new { bgColorIndex = u.BgColorDefault,Name=u.Name });

            return Success(userInfo);
        }

        public ResponseData SetBgColor(string id)
        {
            int num = DbHelper.SqlDSN.ExecuteSql($"update XT_User set bgColorDefault='{id}' where id=1");
            if (num > 0)
            {
                return Success("主题选择成功！");
            }
            else
            {
                return Fail("主题后台配置失败，请联系管理员！");
            }
        }

        /// <summary>
        /// 递归处理数据
        /// </summary>
        /// <param name="childreDataList"></param>
        /// <param name="rootNode"></param>
        public static void GetTreeNodeListByNoLockedDTOArray(XtMenu[] xtMenuEntities, SystemMenu rootNode)
        {
            if (xtMenuEntities == null || xtMenuEntities.Count() <= 0)
            {
                return;
            }

            var childreDataList = xtMenuEntities.Where(p => p.IsLogo == false && p.IsHome == false && p.Pid == rootNode.Id).OrderBy(p => p.Sort).ToList();
            if (childreDataList != null && childreDataList.Count() > 0)
            {
                rootNode.Child = new List<SystemMenu>();

                foreach (var item in childreDataList)
                {
                    SystemMenu treeNode = new SystemMenu()
                    {
                        Id = item.Id,
                        Icon = item.Icon,
                        Href = item.Href,
                        Title = item.Title,
                    };
                    rootNode.Child.Add(treeNode);
                }

                foreach (var item in rootNode.Child)
                {
                    GetTreeNodeListByNoLockedDTOArray(xtMenuEntities, item);
                }
            }
        }
    }
}
